﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Models;
using SenerPro;

namespace MensUnoShop.Filters
{
    /// <summary>
    /// 過濾賣場相關屬性，並建立賣場及當下類別物件
    /// </summary>
    public class ComboMarketFilter : ActionFilterAttribute
    {
        private Logger _log;

        public ComboMarketFilter()
        {
            _log = new Logger(Config.logpath);
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            NormalMarket mk = null;
            Guid mkguid;
            int tid = 0;
            int.TryParse(filterContext.ActionParameters["tagid"] == null ? "" : filterContext.ActionParameters["tagid"].ToString(), out tid);
            List<Tag> navlist = new List<Tag>();
            string mksn = filterContext.ActionParameters["mksn"] as string;
            string pdtag = filterContext.ActionParameters["pdtag"] as string;
            
            //沒有comboID就導回單品頁
            if (tid == 0)
            {
                filterContext.Result = new RedirectResult("/market/n/" + mksn);
            }
            else
            {
                //建立賣場物件
                mk = MarketFactory.GetNormalMarket(mksn);
                if (mk != null)
                {
                    //判斷商品ID正不正確
                    if (!mk.PRODUCTLIST.Exists(p => p.TITLETAGID == int.Parse(pdtag)))
                    {
                        filterContext.Result = new RedirectResult("/market/combo/" + mk.SN + "/" + mk.PRODUCTLIST.First().TITLETAGID + "/" + tid.ToString());
                    }
                    else
                    {
                        mk.TYPE = Config.MarketType.Combo;
                        mk.TAGID = tid;
                        filterContext.Controller.ViewData["market"] = mk;

                        Tag tag = TagFactory.GetTag(mk.TAGID);
                        if (tag != null)
                        {
                            navlist = TagFactory.GetComboNavTagList(mk.TAGID, mk.MKGUID);
                            tag.PARENTID = navlist[0].ID;
                            filterContext.Controller.ViewData["navlist"] = navlist;
                            filterContext.Controller.ViewData["currenttag"] = tag;
                        }
                        else
                        {
                            _log.RecordException(_log.FlowLogPath, "tag is not correct");
                            filterContext.Result = new RedirectResult("/");
                        }
                    }
                }
                else
                {
                    _log.RecordException(_log.FlowLogPath, "market is not correct");
                    filterContext.Result = new RedirectResult("/");
                }
            }

            base.OnActionExecuting(filterContext);
        }
    }
    
    

   

}